﻿#include "DraggableWindow.h"

#include <QSizePolicy>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QMouseEvent>

DraggableWindow::DraggableWindow(QWidget *parent) : DraggableWidget(parent)
  , m_bHeaderPressed(false)
  , m_pTitleBar(Q_NULLPTR)
  , m_pWindowMainLayout(Q_NULLPTR)
  , m_sRestoreSize(QSize())
{
    QSize minSize(600, 400);
    setMinimumSize(minSize);
    resize(minSize);
    m_pTitleBar = new WindowTitleBar(this);
    connect(m_pTitleBar, SIGNAL(windowTitleBarInitialized()), this, SLOT(onInitialize()));
}

DraggableWindow::~DraggableWindow()
{
    if (m_pTitleBar)
    {
        m_pTitleBar->close();
        delete m_pTitleBar;
        m_pTitleBar = Q_NULLPTR;
    }
}

void DraggableWindow::mouseMoveEvent(QMouseEvent *event)
{
    if ((isMaximized() && !m_bHeaderPressed) || (windowState() == Qt::WindowFullScreen))
    {
        DraggableWidget::mouseMoveEvent(event);
        return;
    }
    if (isMaximized() && m_bHeaderPressed)
    {
        QRect m_currentRect = QRect();
        QDesktopWidget desktop;
        if (event->pos().x() <= (width() / 3))
        {
            m_currentRect.setLeft(0);
        }
        else if (event->pos().x() >= (width() / 3 * 2))
        {
            m_currentRect.setLeft(desktop.availableGeometry().width() - m_sRestoreSize.width());
        }
        else
        {
            m_currentRect.setLeft((desktop.availableGeometry().width() / 2) - (m_sRestoreSize.width() / 2));
        }
        m_currentRect.setTop(0);
        m_currentRect.setSize(m_sRestoreSize);
        showNormal();
        setGeometry(m_currentRect);
    }
    DraggableWidget::mouseMoveEvent(event);
}

QWidget *DraggableWindow::windowTitleBar() const
{
    return m_pTitleBar ? m_pTitleBar : Q_NULLPTR;
}

void DraggableWindow::setMainWidget(QWidget *m_widget)
{
    if (m_widget && m_pTitleBar)
    {
        m_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        int index = m_pWindowMainLayout->indexOf(m_pTitleBar);
        m_pWindowMainLayout->insertWidget(index + 1, m_widget);
    }
}

void DraggableWindow::moveWindowToDesktopCenter()
{
    if (isMaximized() || (windowState() == Qt::WindowFullScreen))
    {
        return;
    }
    QDesktopWidget desktop;
    move((desktop.availableGeometry().width() - width()) / 2
         , (desktop.availableGeometry().height() - height()) / 2);
}

void DraggableWindow::onIconButtonClicked()
{
    QMessageBox::about(this, "Button clicked", "You have just clicked the icon button.");
}

void DraggableWindow::onMinimizeButtonClicked()
{
    showMinimized();
}

void DraggableWindow::onMaximizeButtonClicked()
{
    if (isMaximized())
    {
        showNormal();
    }
    else
    {
        m_sRestoreSize = size();
        showMaximized();
    }
}

void DraggableWindow::onCloseButtonClicked()
{
    close();
}

void DraggableWindow::onHeaderPressed()
{
    m_bHeaderPressed = true;
}

void DraggableWindow::onHeaderReleased()
{
    m_bHeaderPressed = false;
}

void DraggableWindow::onInitialize()
{
    m_pWindowMainLayout = new QVBoxLayout(this);
    m_pWindowMainLayout->setContentsMargins(0, 0, 0, 0);
    m_pWindowMainLayout->setSpacing(0);
    setLayout(m_pWindowMainLayout);
    m_pWindowMainLayout->addWidget(m_pTitleBar);
    m_pWindowMainLayout->addStretch();
    connect(m_pTitleBar, SIGNAL(onMousePressed()), this, SLOT(onHeaderPressed()));
    connect(m_pTitleBar, SIGNAL(onMouseReleased()), this, SLOT(onHeaderReleased()));
    connect(m_pTitleBar, SIGNAL(onMouseDoubleClicked()), this, SLOT(onMaximizeButtonClicked()));
    connect(m_pTitleBar, SIGNAL(onIconButtonClicked()), this, SLOT(onIconButtonClicked()));
    connect(m_pTitleBar, SIGNAL(onMinimizeButtonClicked()), this, SLOT(onMinimizeButtonClicked()));
    connect(m_pTitleBar, SIGNAL(onMaximizeButtonClicked()), this, SLOT(onMaximizeButtonClicked()));
    connect(m_pTitleBar, SIGNAL(onCloseButtonClicked()), this, SLOT(onCloseButtonClicked()));
    m_pTitleBar->show();
}
